package com.whsxt.sys.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.common.util.ConstantUtil;
import com.whsxt.common.util.MenuBuildUtil;
import com.whsxt.common.util.WebContextUtil;
import com.whsxt.sys.data.MenuData;
import com.whsxt.sys.data.PermissionData;
import com.whsxt.sys.domain.Permission;
import com.whsxt.sys.domain.User;
import com.whsxt.sys.service.IPermissionService;
import com.whsxt.sys.service.IRoleService;

import cn.hutool.core.util.StrUtil;

@RequestMapping("/sys/menu")
@RestController
public class MenuController {
	
	@Autowired
	private IPermissionService iPermissionService;
	
	@Autowired
	private IRoleService iRoleService;
	/**
	 * 主页面json数据
	 * @return
	 */
	@RequestMapping("/menu")
	public Object getMenu() {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("type", ConstantUtil.TYPE_MENU);
		queryWrapper.eq("available",ConstantUtil.AVAILABLE_TRUE);
		User user = (User) WebContextUtil.getSession().getAttribute("currUser");
		List<Permission> list = null;
		if(user.getType() == 0) {
			list = iPermissionService.list(queryWrapper);
		}else {
			Integer uid = user.getId();
			List<Integer> roleList = iRoleService.selectRidByUid(uid);
			//根据角色查询 对应的 菜单id
			List<Integer> perList = iRoleService.selectPidByRid(roleList);
			//去重
			Set<Integer> set = new HashSet<Integer>();
			set.addAll(perList);
			if(set != null && set.size()>0) {
				queryWrapper.in("id", set);
				//跟菜单 id 查询对应的菜单 返回给前台
				list = iPermissionService.list(queryWrapper);
			}else {
				list = new ArrayList<Permission>();
			}
		}
		List<MenuData> menuDataList = new ArrayList<MenuData>();
		for (Permission p : list) {
			MenuData menuData = new MenuData(p.getId(), p.getPid(), p.getTitle(),p.getIcon(),p.getHref(),p.getOpen()==1?true:false);
			menuDataList.add(menuData);
		}
		List<MenuData> menuBuildJson = MenuBuildUtil.MenuBuildJson(menuDataList, ConstantUtil.PID_MENU);
		return new Result(menuBuildJson);
	}
	
	/**************菜单*****************/
	/**
	 * 菜单树行结构
	 */
	@RequestMapping("/dtree")
	public Object dtree() {
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("type", ConstantUtil.TYPE_MENU);
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		List<Permission> list = iPermissionService.list(queryWrapper );
		List<MenuData> datas = new ArrayList<MenuData>();
		for (Permission permission : list) {
			Boolean spread = permission.getOpen()==1?true:false;
			datas.add(new MenuData(permission.getId(), permission.getPid(), permission.getTitle(), spread ));
		}
		return new Result(datas);
	}
	/**
	 *  菜单列表
	 * @param data
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(PermissionData data) {
		IPage<Permission> page = new Page<Permission>(data.getPage(),data.getLimit());
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("type", ConstantUtil.TYPE_MENU);
		queryWrapper.eq("available", ConstantUtil.AVAILABLE_TRUE);
		queryWrapper.like(StrUtil.isNotBlank(data.getTitle()), "title", data.getTitle());
		queryWrapper.and(data.getId() !=null, 
				i -> 
				i.eq("id", data.getId()).or().eq("pid", data.getId()));
		queryWrapper.orderByAsc("ordernum");
		iPermissionService.page(page, queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	
	/**
	 * 获取最大的排序码
	 * @return
	 */
	@RequestMapping("/getOrdernum")
	public Map<String, Object> getOrdernum() {
		Map<String, Object> map = new HashMap<String, Object>();
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.orderByDesc("ordernum");
		IPage<Permission> page = new Page<Permission>(1, 1);
		iPermissionService.page(page, queryWrapper);
		List<Permission> records = page.getRecords();
		if(records.size()>0) {
			map.put("value",records.get(0).getOrdernum()+1);
		}else {
			map.put("value", 1);
		}
		return map;
	}
	/**
	 * 菜单新增
	 * @param data
	 * @return
	 */
	@RequestMapping("/addMenu")
	public Object addDept(PermissionData data) {
		try {
			data.setType(ConstantUtil.TYPE_MENU);
			iPermissionService.save(data);
			return new Result(CodeMsg.MENU_ADD_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.MENU_ADD_ERROR);
		}
	}
	/**
	 * 更新菜单数据
	 * @param data
	 * @return
	 */
	@RequestMapping("/updateMenu")
	public Object updateDept(PermissionData data) {
		try {
			iPermissionService.updateById(data);
			return new Result(CodeMsg.MENU_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.MENU_UPDATE_ERROE);
		}
	}
	/**
	 * 判断删除的节点是否有子节点
	 * @param id
	 * @return
	 */
	@RequestMapping("/ishaschild")
	public Map<String, Object> ishaschild(Integer id) {
		Map<String, Object> map = new HashMap<String, Object>();
		QueryWrapper<Permission> queryWrapper = new QueryWrapper<Permission>();
		queryWrapper.eq("pid", id);
		int count = iPermissionService.count(queryWrapper);
		if(count != 0) {
			map.put("value", true);
		}else {
			map.put("value", false);
		}
		return map;
	}
	/**
	 * 删除单个数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iPermissionService.removeById(id);
			return new Result(CodeMsg.MENU_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.MENU_DELETE_ERROE);
		}
	}
}
